package cn.wolfcode.transfer.service.impl;

import cn.wolfcode.transfer.domain.Permission;
import cn.wolfcode.transfer.mapper.PermissionMapper;
import cn.wolfcode.transfer.qo.QueryObject;
import cn.wolfcode.transfer.service.IPermissionService;
import cn.wolfcode.transfer.util.RequiredPermission;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
public class PermissionServiceImpl implements IPermissionService {
    @Autowired
    private PermissionMapper PermissionMapper;
    @Autowired
    //获取上下文对象
    private ApplicationContext ctx;
//    @Autowired
//    private RequestMappingHandlerMapping handlerMapping;
    @Override
    public void reload() {
//        Map<RequestMappingInfo, HandlerMethod> handlerMethods = handlerMapping.getHandlerMethods();
//        Collection<HandlerMethod> values = handlerMethods.values();
//        for (HandlerMethod method:values)
//        {
//            RequiredPermission methodAnnotation = method.getMethodAnnotation(RequiredPermission.class);
//
//        }
               List<String> permissions = PermissionMapper.selectAllExpression();

        //通过上下文对象获取贴了Controller标签的bean
        Map<String, Object> beansWithAnnotation = ctx.getBeansWithAnnotation(Controller.class);
        Collection<Object> beans = beansWithAnnotation.values();
        Long i = 0L ;
        for (Object bean : beans){
            Class<?> aClass = bean.getClass();
            Method[] declaredMethods = aClass.getDeclaredMethods();
            for (Method method:declaredMethods){
                RequiredPermission annotation = method.getAnnotation(RequiredPermission.class);
                if (annotation==null){
                    continue;
                }
                String name = annotation.name();
                String expression = annotation.expression();
                i++;
                if (permissions.contains(expression)){
                    continue;
                }
                PermissionMapper.insert(i,name,expression);
                permissions.add(expression);
            }
        }
    }

    @Override
    public int insert(Permission record) {
        int insert = PermissionMapper.insert(record);
        return insert;
    }

    @Override
    public int deleteByPrimaryKey(Long id) {
        int i = PermissionMapper.deleteByPrimaryKey(id);
        return i;
    }




    @Override
    public List<Permission> selectAll() {
        List<Permission> Permissions = PermissionMapper.selectAll();
        return Permissions;
    }

    @Override
    public PageInfo<Permission> getPageInfo(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Permission> Permissions = PermissionMapper.selectAll();
        return new PageInfo<Permission>(Permissions);
    }

    @Override
    public List<String> selectByEmployeeId(Long id) {
        List<String> strings = PermissionMapper.selectByEmployeeId(id);
        return strings;
    }
}
